package com.ds.dsm.repository.entity;

import com.ds.common.JDSException;
import com.ds.common.util.ClassUtility;
import com.ds.dsm.aggregation.api.method.MethodAPITree;
import com.ds.dsm.aggregation.module.service.WinConfigTree;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.enums.ModuleViewType;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.tree.annotation.TreeAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.repository.config.EntityConfig;
import com.ds.esd.dsm.repository.config.FieldEntityConfig;
import com.ds.esd.dsm.view.field.FieldModuleConfig;
import com.ds.esd.tool.ui.component.list.TreeListItem;
import com.ds.esd.tool.ui.enums.ComponentType;

import java.util.List;


@TreeAnnotation()
public class EntityConfigTree extends TreeListItem {

    public EntityConfigTree(String sourceClassName) throws JDSException {
        super("RepositoryConfigTree", "Repository配置", "spafont spa-icon-c-webapi");
        this.setEuClassName("dsm.esdentity.config.repository.RepositoryInfo");
        EntityConfig repositoryConfig = DSMFactory.getInstance().getRepositoryManager().getEntityConfig(sourceClassName,  false);
        this.setIniFold(false);
        List<MethodConfig> customMethods = repositoryConfig.getOtherMethodsList();
        for (MethodConfig methodAPIBean : customMethods) {
            if (methodAPIBean.getView() != null) {
                this.addChild(new EntityConfigTree(methodAPIBean));
            } else {
                this.addChild(new MethodAPITree(methodAPIBean));
            }
        }

        List<FieldEntityConfig> FieldEntityConfigs = repositoryConfig.getFieldList();
        for (FieldEntityConfig FieldEntityConfig : FieldEntityConfigs) {
            this.addChild(new EntityConfigTree(FieldEntityConfig, "", sourceClassName));
        }

        List<FieldModuleConfig> moduleMethods = repositoryConfig.getModuleMethods();
        for (FieldModuleConfig fieldModuleConfig : moduleMethods) {
            this.addChild(new EntityConfigTree(fieldModuleConfig, sourceClassName));
        }
    }


    public EntityConfigTree(MethodConfig methodAPIBean) {
        ModuleViewType dsmType = methodAPIBean.getModuleViewType();
        String methodName = methodAPIBean.getMethodName();
        CustomMenuItem customMenuItem = methodAPIBean.getDefaultMenuItem();
        if (methodAPIBean.getCaption() != null && !methodAPIBean.getCaption().equals("")) {
            this.caption = methodAPIBean.getCaption() + "(" + methodAPIBean.getName() + ")";
        } else {

            if (customMenuItem != null) {
                this.caption = methodName + "(" + methodAPIBean.getName() + ")-" + methodAPIBean.getModuleViewType().getName();
            } else {
                this.caption = methodName + "(" + dsmType.getName() + ")-" + methodAPIBean.getModuleViewType().getName();
            }
        }
        this.tips = methodAPIBean.getCaption() + "(" + methodAPIBean.getModuleViewType().getName() + ")<br/>";
        this.tips = tips + methodName + "(" + dsmType.getName() + ")";
        this.imageClass = dsmType.getImageClass();
        this.setIniFold(false);

        this.setEuClassName(dsmType.getClassName());
        this.setId(dsmType.getType() + methodName);
        this.setGroup(true);
        this.addTagVar("sourceClassName", methodAPIBean.getSourceClassName());
        this.addTagVar("methodName", methodName);
        this.addTagVar("domainId", methodAPIBean.getDomainId());
        this.addTagVar("viewInstId", methodAPIBean.getViewInstId());
        this.addChild(new WinConfigTree(methodAPIBean));
        this.addChild(new MethodAPITree(methodAPIBean));

    }

    public EntityConfigTree(FieldEntityConfig repositoryConfig, String methodName, String sourceClassName) {
        this.caption = repositoryConfig.getCaption();
        if (caption == null || caption.equals("")) {
            caption = repositoryConfig.getFieldname();
        }
        this.setImageClass(repositoryConfig.getComponentType().getImageClass());
        this.setEuClassName("dsm.esdentity.config.form.field.FieldNav");
        try {
            Class sourceClass = ClassUtility.loadClass(sourceClassName);
            this.setId(sourceClass.getSimpleName() + "_" + methodName + "_" + repositoryConfig.getFieldname());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        this.setIniFold(false);
        this.addTagVar("sourceClassName", sourceClassName);

        this.addTagVar("methodName", methodName);
        this.addTagVar("fieldname", repositoryConfig.getFieldname());
    }

    public EntityConfigTree(FieldModuleConfig moduleConfig, String sourceClassName) {
        this.caption = moduleConfig.getCaption();
        if (caption == null || caption.equals("")) {
            caption = moduleConfig.getFieldname();
        }
        try {
            EntityConfig repositoryConfig = DSMFactory.getInstance().getRepositoryManager().getEntityConfig(moduleConfig.getMethodConfig().getEntityClassName(),  false);
            List<FieldEntityConfig> FieldEntityConfigs = repositoryConfig.getFieldMethods();
            for (FieldEntityConfig FieldEntityConfig : FieldEntityConfigs) {
                this.addChild(new EntityConfigTree(FieldEntityConfig, moduleConfig.getMethodName(), sourceClassName));
            }
        } catch (JDSException e) {
            e.printStackTrace();
        }
        this.setImageClass(ComponentType.Module.getImageClass());
        this.setEuClassName("dsm.esdentity.config.repository.field.FieldList");
        this.setId(sourceClassName + "_" + moduleConfig.getMethodName() + "_" + moduleConfig.getFieldname());
        this.setIniFold(false);
        this.addTagVar("sourceClassName", sourceClassName);
        this.addTagVar("methodName", moduleConfig.getMethodName());
        this.addTagVar("fieldname", moduleConfig.getFieldname());
    }


}


